package weaver.interfaces.zhlx.duss.action.salesmodule.crm;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.CrmUtil;
import weaver.interfaces.zhlx.duss.utils.PropUltraUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年12月23日 09:02
 */
public class Wea01CusSpecApproPlanPhaseAction extends BaseBean implements Action {
    private static final String RES_CODE_ZERO = "0";

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>Wea01CusSpecApproPlanPhaseAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            String message = "调用CRM系统接口返回:";
            CrmUtil crmUtil = new CrmUtil();
            rs.executeQuery("select sqdbh,sqrgh,sqrq,qy,gdrq,gpdwbwlz1,djdwbwlz1,wwkhbm,generaldescription1," +
                    "glsjid,tempmm,cuspartno,cusspecno,producttype,diameter," +
                    "dopant,crystalorientation,pullingmethod,res from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                String sqdbh = Util.null2String(rs.getString("sqdbh"));//申请单编号
                String sqrgh = Util.null2String(rs.getString("sqrgh"));//申请人工号
                String sqrq = Util.null2String(rs.getString("sqrq"));//申请日期
                String qy = Util.null2String(rs.getString("qy"));//区域
                String gdrq = Util.null2String(rs.getString("gdrq"));//归档日期
                String gpdwbwlz1 = Util.null2String(rs.getString("gpdwbwlz1"));//硅片的外部物料组
                String djdwbwlz1 = Util.null2String(rs.getString("djdwbwlz1"));//单晶的外部物料组
                String wwkhbm = Util.null2String(rs.getString("wwkhbm"));//五位客户编码
                String generaldescription1 = Util.null2String(rs.getString("generaldescription1"));//General Description
                String glsjid = Util.null2String(rs.getString("glsjid"));//关联商机ID
                String tempmmname = Util.null2String(rs.getString("tempmm"));//Temp.MM
                String cuspartno = Util.null2String(rs.getString("cuspartno"));//cus part No
                String cusspecno = Util.null2String(rs.getString("cusspecno"));//cus spec No
                String producttype = Util.null2String(rs.getString("producttype"));//产品类型
                String diameter = Util.null2String(rs.getString("diameter"));//直径
                String dopant = Util.null2String(rs.getString("dopant"));//掺杂剂
                String crystalorientation = Util.null2String(rs.getString("crystalorientation"));//晶向
                String pullingmethod = Util.null2String(rs.getString("pullingmethod"));//拉晶方式
                String res = Util.null2String(rs.getString("res"));//电阻率

                JSONObject dataJson = new JSONObject();
                JSONObject object_data = new JSONObject();
                object_data.put("dataObjectApiName", "spec_review_size12_02__c");
                object_data.put("review_no__c", sqdbh);
                object_data.put("applied_employee_id__c", sqrgh);
                sqrq = !"".equals(sqrq) ? sqrq + " 00:00:00" : sqrq;
                object_data.put("application_date__c", crmUtil.dateToStamp(sqrq));
                object_data.put("region__c", qy);
                object_data.put("process_start_time__c", crmUtil.dateToStamp(sqrq));
                gdrq = !"".equals(gdrq) ? gdrq + " 00:00:00" : gdrq;
                object_data.put("process_end_time__c", crmUtil.dateToStamp(gdrq));
                object_data.put("silicon_external_module__c", gpdwbwlz1);
                object_data.put("crystal_external_module__c", djdwbwlz1);
                object_data.put("account_no__c", wwkhbm);
                object_data.put("customer_material_descript__c", generaldescription1);
                object_data.put("business_opportunity__c", glsjid);
                object_data.put("temp_part_number__c", tempmmname);
                object_data.put("customer_part_code__c", cuspartno);
                object_data.put("customer_spec_num__c", cusspecno);
                object_data.put("processed_product_type__c", producttype);
                object_data.put("diameter__c", diameter);
                object_data.put("blend_material__c", dopant);
                object_data.put("crystal_orientation__c", crystalorientation);
                object_data.put("growth_method__c", pullingmethod);
                object_data.put("resistivity__c", res);
                JSONObject propVal = PropUltraUtil.getPropMainVal("zhlxcrm");
                String username = propVal.getString("username");
                object_data.put("is_deleted", false);
                object_data.put("owner", username.split(","));
                object_data.put("ec_approvalStatus__c", "3");
                object_data.put("ec_processid__c", sqdbh);
                object_data.put("record_type", "record_95KTm__c");
				dataJson.put("skipDataStatusValidate", true);
                dataJson.put("object_data", object_data);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "Wea01CusSpecApproPlanPhaseAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>Wea01CusSpecApproPlanPhaseAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = crmUtil.excuteHandler(dataJson, dataMap, "Wea01CusSpecApproPlanPhaseAction");
                writeLog(">>>>>Wea01CusSpecApproPlanPhaseAction>>>>>resultJson=" + resultJson);
                String resultMessage = Util.null2String(resultJson.getString("message"));
                if (!"success".equals(resultMessage)) {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    JSONObject resultJsonObj = resultJson.getJSONObject("data");
                    String errorCode = resultJsonObj.getString("errorCode");
                    if (!RES_CODE_ZERO.equals(errorCode)) {
                        message += Util.null2String(resultJsonObj.getString("errorMessage"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>Wea01CusSpecApproPlanPhaseAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
